package com.accp.hotevent.service;


import com.accp.hotevent.dao.CommentDao;
import com.accp.hotevent.entity.Comment;
import com.accp.hotevent.entity.Event;
import com.accp.hotevent.entity.Notice;
import com.accp.hotevent.websocket.NoticeServer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Autowired
    CommentDao commentDao;

    @Autowired
    EventService eventService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    NoticeServer noticeServer;

    public PageInfo<Comment> getCommentsList(Integer pageNum,Integer pageSize,Comment comment){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Comment> pageInfo =new PageInfo<>(commentDao.getCommentsList(comment));
        return pageInfo;
    }


    public boolean add(Comment comment){
        try {
            comment.setCreateTime(new Date());
            commentDao.add(comment);

            //通知
            Event event = eventService.getOne(comment.getEventId());
            Notice notice = Notice.builder()
                    .userId(event.getUser().getId())
                    .content("<<"+event.getTitle()+">> 有新的评论")
                    .build();

            noticeService.add(notice);
            noticeServer.sendInfo(notice);

            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
